var password_protect = {

	openRequestedPopup: function() {
		var windowObjectReference;

	  windowObjectReference = window.open("chrome://password-protect/content/window.xul",
  	  "password-protect-password-generator-window",
    	"resizable=no,scrollbars=yes,status=yes,width=720,height=560,chrome");
	},

	getURL: function() {
		var url = document.getElementById("urlbar-container").childNodes[0].value;

	  if (url != "")
	  {
  	  double_slashes = url.search("//");
    	url = url.substr(double_slashes + 2);

	    length = url.length - url.search("/");
  	  url = url.substr(0, url.search("/"));

	    var pos = url.indexOf(".");

  	  var temp_url_1 = url;
    	var temp_url_2 = url.substring(pos + 1);

	    pos = temp_url_2.indexOf(".");

  	  while (pos != -1)
    	{
      	temp_url_1 = temp_url_2;
	      temp_url_2 = temp_url_2.substring(pos + 1);
  	    pos = temp_url_2.indexOf(".");
    	}

	    return temp_url_1;
	  }

	  return "";
	},

	addSubmitListener: function()
	{
  	var tooltiptext = " - Click here to open Secure Password Generator";

	  window.addEventListener("submit", password_protect.submitHandler, false);

  	document.getElementById("password-protect-toolbarbutton").setAttribute("style", "list-style-image: url(\"chrome://password-protect/skin/toolbar-large-set.png\");");
	  document.getElementById("password-protect-toolbarbutton").setAttribute("state", "set");
  	document.getElementById("password-protect-toolbarbutton").setAttribute("tooltiptext", "PasswordProtect is ACTIVE" + tooltiptext);
	},

	removeSubmitListener: function()
	{
  	var tooltiptext = " - Click here to open Secure Password Generator";
	  window.removeEventListener("submit", password_protect.submitHandler, false);

  	document.getElementById("password-protect-toolbarbutton").setAttribute("style", "list-style-image: url(\"chrome://password-protect/skin/toolbar-large-unset.png\");");
	  document.getElementById("password-protect-toolbarbutton").setAttribute("state", "unset");
  	document.getElementById("password-protect-toolbarbutton").setAttribute("tooltiptext", "PasswordProtect is NOT ACTIVE" + tooltiptext);
	},

	handleListener: function()
	{
  	var state;
	  state = document.getElementById("password-protect-toolbarbutton").getAttribute("state");

  	if (state == "set")
    	password_protect.removeSubmitListener();
	  else
  	  password_protect.addSubmitListener();
	},

	submitHandler: function()
	{
	  var inputs = content.document.getElementsByTagName("input");

		var length = inputs.length;
		var i = 0;

  	for (input = 0;input < length; input++)
	  {
    	if (content.document.getElementsByTagName("input")[input].getAttribute("type") == "password")
  	  {
	      var concat = password_protect.getURL() + content.document.getElementsByTagName("input")[input].value;

	      var i = 0;
      	for (i = 0; i < 10000; i++)
    	  {
  	      concat = Sha1.hash(concat);
	      }
      	content.document.getElementsByTagName("input")[input].value = concat;
    	}
  	}
	}

}
